/**Spring Security配置文件，用于保护资源
 */
package com.example.demoauthservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configuration.ResourceServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configurers.ResourceServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.token.RemoteTokenServices;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;

@Configuration
@EnableResourceServer
@EnableGlobalMethodSecurity(prePostEnabled = true,securedEnabled = true)
public class MyResourceConfig  extends ResourceServerConfigurerAdapter {



    //配置服务保护规则
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http .csrf().disable()
                .exceptionHandling()
                .and()
                .authorizeRequests()
                .antMatchers("/product/**").permitAll()
                .antMatchers("/order/**").hasRole("USER")// 配置order访问控制，必须认证后才可以访问
                .anyRequest().authenticated()                       //配置所有请求访问控制，必须认证后才可以访问
                .and()
                .cors()                                             //支持跨域访问，implicit授权模式访问时须配
                .and()
                .httpBasic();
    }

    /*与yml文件中secutity的相关配置等效，主要是用于验证token有效性
        RemoteTokenServices tokenServices() {
        RemoteTokenServices services = new RemoteTokenServices();
        services.setCheckTokenEndpointUrl("http://localhost:8770/oauth/check_token");      //验证token接口地址
        services.setClientId("client_1");
        services.setClientSecret("mysecret");
        return services;
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) {
        resources.resourceId("resource_1");           //本resource的id
        //...... 还可以有有其他的配置
    }*/
}
